import { Order } from 'blockly/javascript';
import * as Blockly from 'blockly';

// 异步块的代码生成器
export const asyncFunctionGenerators = {

  // 修改函数定义块生成器
  'procedures_defreturn': function (block: Blockly.Block, generator: any) {
    // 获取函数名和参数
    const funcName = generator.nameDB_.getName(
      block.getFieldValue('NAME'), Blockly.PROCEDURE_CATEGORY_NAME);
    const args = [];
    const variables = block.getVars ? block.getVars() : [];

    for (let i = 0; i < variables.length; i++) {
      args[i] = generator.nameDB_.getName(variables[i],
        Blockly.VARIABLE_CATEGORY_NAME);
    }

    // 获取函数体代码
    let code = generator.statementToCode(block, 'STACK');
    const returnValue = generator.valueToCode(block, 'RETURN',
      Order.NONE) || '';
    if (returnValue) {
      code += 'return ' + returnValue + ';\n';
    }

    // 检查是否为异步函数
    const isAsync = block.getFieldValue('ASYNC') === 'TRUE';
    const asyncPrefix = isAsync ? 'async ' : '';

    // 生成函数定义代码
    code = asyncPrefix + 'function ' + funcName + '(' + args.join(', ') + ') {\n' +
      code + '}';

    return code;
  },

  // 无返回值函数定义块生成器
  'procedures_defnoreturn': function (block: Blockly.Block, generator: any) {
    // 获取函数名和参数
    const funcName = generator.nameDB_.getName(
      block.getFieldValue('NAME'), Blockly.PROCEDURE_CATEGORY_NAME);
    const args = [];
    const variables = block.getVars ? block.getVars() : [];

    for (let i = 0; i < variables.length; i++) {
      args[i] = generator.nameDB_.getName(variables[i],
        Blockly.VARIABLE_CATEGORY_NAME);
    }

    // 获取函数体代码
    let code = generator.statementToCode(block, 'STACK');

    // 检查是否为异步函数
    const isAsync = block.getFieldValue('ASYNC') === 'TRUE';
    const asyncPrefix = isAsync ? 'async ' : '';

    // 生成函数定义代码
    code = asyncPrefix + 'function ' + funcName + '(' + args.join(', ') + ') {\n' +
      code + '}';

    return code;
  },

  // 函数调用块生成器
  'procedures_callreturn': function (block: Blockly.Block, generator: any) {
    // 获取函数名和参数
    const funcName = generator.nameDB_.getName(
      block.getFieldValue('NAME'), Blockly.PROCEDURE_CATEGORY_NAME);
    const args = [];

    // 获取参数
    for (let i = 0; i < block.inputList.length; i++) {
      const input = block.inputList[i];
      if (input.name && input.name.startsWith('ARG')) {
        args.push(generator.valueToCode(block, input.name, Order.NONE) || 'null');
      }
    }

    // 构建基本函数调用
    let code = funcName + '(' + args.join(', ') + ')';

    // 检查是否使用 await
    console.log('Block:', block);
    console.log('AWAIT field value:', block.getFieldValue('AWAIT'));

    if (block.getFieldValue('AWAIT') === 'TRUE') {
      console.log('Adding await prefix');
      code = 'await ' + code;
    }

    console.log('Generated code:', code);

    return [code, Order.FUNCTION_CALL];
  },

  // 无返回值函数调用块生成器
  'procedures_callnoreturn': function (block: Blockly.Block, generator: any) {
    // 获取函数名和参数
    const funcName = generator.nameDB_.getName(
      block.getFieldValue('NAME'), Blockly.PROCEDURE_CATEGORY_NAME);
    const args = [];

    // 获取参数
    for (let i = 0; i < block.inputList.length; i++) {
      const input = block.inputList[i];
      if (input.name && input.name.startsWith('ARG')) {
        args.push(generator.valueToCode(block, input.name, Order.NONE) || 'null');
      }
    }

    // 构建基本函数调用
    let code = funcName + '(' + args.join(', ') + ')';

    // 检查是否使用 await
    if (block.getFieldValue('AWAIT') === 'TRUE') {
      code = 'await ' + code;
    }

    // 无返回值的函数调用需要添加分号和换行符
    return code + ';\n';
  },

  // 方法定义块生成器
  'method_definition': function (block: Blockly.Block, generator: any) {
    // 获取方法名和参数
    const methodName = generator.nameDB_.getName(
      block.getFieldValue('NAME'), Blockly.PROCEDURE_CATEGORY_NAME);
    const args = [];
    const variables = block.getVars ? block.getVars() : [];

    for (let i = 0; i < variables.length; i++) {
      args[i] = generator.nameDB_.getName(variables[i],
        Blockly.VARIABLE_CATEGORY_NAME);
    }

    // 获取方法体代码
    let code = generator.statementToCode(block, 'STACK');
    const returnValue = generator.valueToCode(block, 'RETURN',
      Order.NONE) || '';
    if (returnValue) {
      code += 'return ' + returnValue + ';\n';
    }

    // 检查是否为异步方法
    const isAsync = block.getFieldValue('ASYNC') === 'TRUE';
    const asyncPrefix = isAsync ? 'async ' : '';

    // 生成方法定义代码
    code = asyncPrefix + methodName + '(' + args.join(', ') + ') {\n' +
      code + '}';

    return code;
  },

  // 方法调用块生成器
  'method_call': function (block: Blockly.Block, generator: any) {
    // 获取方法名和参数
    const methodName = generator.nameDB_.getName(
      block.getFieldValue('NAME'), Blockly.PROCEDURE_CATEGORY_NAME);
    const args = [];

    // 获取参数
    for (let i = 0; i < block.inputList.length; i++) {
      const input = block.inputList[i];
      if (input.name && input.name.startsWith('ARG')) {
        args.push(generator.valueToCode(block, input.name, Order.NONE) || 'null');
      }
    }

    // 构建基本方法调用
    let code = methodName + '(' + args.join(', ') + ')';

    // 检查是否使用 await
    if (block.getFieldValue('AWAIT') === 'TRUE') {
      code = 'await ' + code;
    }

    return [code, Order.FUNCTION_CALL];
  },

  // 自定义函数块生成器
  'function_block': function (block: Blockly.Block, generator: any) {
    // 获取函数名和参数
    const funcName = generator.nameDB_.getName(
      block.getFieldValue('NAME'), Blockly.PROCEDURE_CATEGORY_NAME);
    const args = [];
    const variables = block.getVars ? block.getVars() : [];

    for (let i = 0; i < variables.length; i++) {
      args[i] = generator.nameDB_.getName(variables[i],
        Blockly.VARIABLE_CATEGORY_NAME);
    }

    // 获取函数体代码
    let code = generator.statementToCode(block, 'STACK');
    const returnValue = generator.valueToCode(block, 'RETURN',
      Order.NONE) || '';
    if (returnValue) {
      code += 'return ' + returnValue + ';\n';
    }

    // 检查是否为异步函数
    const isAsync = block.getFieldValue('ASYNC') === 'TRUE';
    const asyncPrefix = isAsync ? 'async ' : '';

    // 生成函数定义代码
    code = asyncPrefix + 'function ' + funcName + '(' + args.join(', ') + ') {\n' +
      code + '}';

    return code;
  },

  // 自定义函数调用块生成器
  'function_call_block': function (block: Blockly.Block, generator: any) {
    // 获取函数名和参数
    const funcName = generator.nameDB_.getName(
      block.getFieldValue('NAME'), Blockly.PROCEDURE_CATEGORY_NAME);
    const args = [];

    // 获取参数
    for (let i = 0; i < block.inputList.length; i++) {
      const input = block.inputList[i];
      if (input.name && input.name.startsWith('ARG')) {
        args.push(generator.valueToCode(block, input.name, Order.NONE) || 'null');
      }
    }

    // 构建基本函数调用
    let code = funcName + '(' + args.join(', ') + ')';

    // 检查是否使用 await
    if (block.getFieldValue('AWAIT') === 'TRUE') {
      code = 'await ' + code;
    }

    return [code, Order.FUNCTION_CALL];
  }
}; 